<?php
namespace app\admin\model;
use think\Model;

class Cate extends Model
{
    
    public function cateTree(){
    	$cateres=$this->order('sort asc')->select();
    	return $this->sort($cateres);
    }
    
    public function sort($data,$pid=0,$level=0){
    	static $arr=array();
    	foreach ($data as $k=> $v) {
    		if($v['pid']==$pid){
    			$v['level']=$level;
    			$arr[]=$v;
    			$this->sort($data,$v['id'],$level+1);
    		}
    	}
    	return $arr;
    }
    public function getchildren($cateid){
    	$cateres=$this->select();
    	return $this->_getchildren($cateres,$cateid);
    }

    public function _getchildren($cateres,$cateid){
    	static $arr=array();
    	foreach ($cateres as $k => $v) {
    		if($v['pid']== $cateid){
    			$arr[]=$v['id'];
    			$this->_getchildren($cateres,$v['id']);
    		}
    	}
    	return $arr;
    }

    //获得当前修改栏目的家谱树
    public function getTree($id){
    		static $tree=array();
    		$cates=$this->select();

    	while ($id>0){
    		foreach ($cates as $v) {
    			if($v['id']== $id){
    				$tree[]=$v;

    				$id=$v['pid'];
    				break;
    			}
    		}
    	}
    	return $tree;
    }
}
